package com.infore.statisticsAndAlarm.repository;

import java.util.List;
import java.util.Map;

import javax.persistence.NamedEntityGraph;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.infore.statisticsAndAlarm.entity.KnowledgeBase;

@Repository
public interface IKnowledgeBaseRepository
		extends JpaRepository<KnowledgeBase, String>, JpaSpecificationExecutor<KnowledgeBase> {

	// 按条件查询全部
	@EntityGraph(value = "KnowledgeBase_view")
	List<KnowledgeBase> findAll(Specification<KnowledgeBase> specification);

	@Modifying
	@Transactional
	int deleteByKnowledgeBaseIdIn(List<String> idsList);

	// 按id查询
	@EntityGraph(value = "KnowledgeBase_view")
	List<KnowledgeBase> findByKnowledgeBaseIdIn(List<String> idsList);

	// 查询单个文件
	@EntityGraph(value = "KnowledgeBase_view")
	KnowledgeBase findOneByKnowledgeBaseId(String id);

	// 统计每类文件的数量
	@Query(value = "select t.knowledge_type as type , to_char(count(*) ,'999999' ) as count from KNOWLEDGE_BASE t group by t.knowledge_type", nativeQuery = true)
	List<Map<String, Object>> queryGroupByKnowledgeType();
}
